#include "log_elem.h"

int
creat_log_elem (LogElem **le) {
    if (le == NULL) goto error;
    /* 以全0初始化LogElem */
    *le = g_new0 (LogElem, 1);
    if (*le == NULL) goto error;
    (*le)->extra = g_ptr_array_new ();
    (*le)->str_pool = g_string_chunk_new (128);
    return 0;
    error:
    return -1;
}

int
free_log_elem (LogElem *le) {
    if (le == NULL)
        goto error;
    le->src = NULL;
    le->dest = NULL;
    if (le->extra != NULL) {
        g_ptr_array_free (le->extra, TRUE);
    }
    g_string_chunk_free (le->str_pool);
    g_free (le);
    return 0;
    error:
    return -1;
}

int
clean_log_elem (LogElem *le) {
     if (le == NULL)
        goto error;
    le->src = NULL;
    le->dest = NULL;
    le->dest_type = 0;
    le->src_type = 0;
    le->type = 0;
    g_ptr_array_set_size (le->extra, 0);
    g_string_chunk_clear (le->str_pool);
    return 0;
    error:
    return -1;
}

int
log_elem_populate (LogElem *le, LogElemType type, LogElemPointType src_type,
                                        const char * src, LogElemPointType dest_type, const char *dest) {
    le->type = type;
    le->src_type = src_type;
    le->dest_type = dest_type;
    le->src = g_string_chunk_insert_const (le->str_pool, src);
    le->dest = g_string_chunk_insert_const (le->str_pool, dest);
    return 0;
}

int
log_elem_add_extra (LogElem *le, const char *extra) {
    gchar * str;
    str = g_string_chunk_insert_const (le->str_pool,  extra);
    g_ptr_array_add (le->extra, str);
    return 0;
}